using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Data;

using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.App.Shop.Model;

namespace PlugNT.App.Shop.DAL
{
    public class GoodsAttr 
    {
        internal static string currTableName = WebConfig.TablePrefix + "s_goods_attr";



        public bool Add(GoodsAttrInfo model)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + " (");
            strSql.Append("sys_model_id,goods_id,attr_name,attr_value,attr_price)");
            strSql.Append(" values (");
            strSql.Append(" " + model.sys_model_id.ToString() + ",");
            strSql.Append(" " + model.goods_id.ToString() + ",");
            strSql.Append("'" + StringHelper.SqlFilter(model.attr_name) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.attr_value) + "',");
            strSql.Append(" " + model.attr_price.ToString());
            strSql.Append(")");

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }


        public bool Update(GoodsAttrInfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("goods_id=" + model.goods_id.ToString() + ",");
            strSql.Append("attr_name='" + PlugNT.Safe.StringHelper.SqlFilter(model.attr_name) + "',");
            strSql.Append("attr_value='" + PlugNT.Safe.StringHelper.SqlFilter(model.attr_value) + "',");
            strSql.Append("attr_price=" + model.attr_price.ToString());
            strSql.Append(" where id=" + model.id.ToString());

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }

        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [" + currTableName + "] ");
            strSql.Append(" where id=" + id.ToString());

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }




        private static IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }



        public static DataTable GetList(int sysModelId, string andWhere)
        {
            return DbHelper.GetDataTable(string.Format("select * from [" + currTableName + "] where sys_model_id={0} {1}", sysModelId, andWhere));
        }



        public DataTable GetList(int sysModelId, int topCount, int currPageCount, string andWhere, LeftJoinInfo joininfo)
        {

            string sql = Sql.GetRePagingString(topCount, currPageCount, currTableName, "id", "*", " and sys_model_id=" + sysModelId + andWhere, joininfo);
            return DbHelper.GetDataTable(sql);
        }

        public int GetCount(int sysModelId, string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where sys_model_id=" + sysModelId + andWhere;
            object obj = DbHelper.GetSingle(sql);
            return obj == null ? 0 : int.Parse(obj.ToString());
        }



        public GoodsAttrInfo GetGoodsAttrInfo(int id)
        {
            GoodsAttrInfo model = null;
            IDataReader dr = GetReader(1, "*", " and id=" + id.ToString());
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new GoodsAttrInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.sys_model_id = int.Parse(dr["sys_model_id"].ToString());
                    model.goods_id = int.Parse(dr["goods_id"].ToString());
                    model.attr_name = dr["attr_name"].ToString();
                    model.attr_value = dr["attr_value"].ToString();
                    model.attr_price = double.Parse(dr["attr_price"].ToString());
                }
                dr.Close();
            }
            return model;

        }



    }
}

